package net.jjjshop.shop.controller.lbt;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.jjjshop.common.entity.lbt.LbLineClass;
import net.jjjshop.common.service.lbt.LbLineClassService;
import net.jjjshop.framework.common.api.ApiResult;
import net.jjjshop.framework.util.LoginUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(value = "公排分类", tags = {"index"})
@RestController
@RequestMapping("/shop/line/class")
public class GpLineController {

    @Resource
    private LbLineClassService lineClassService;

    @GetMapping("/classList")
    @ApiOperation(value = "查询全部商家公排分类")
    public ApiResult<?> lineClass(){
      return  ApiResult.ok(lineClassService.selectAll());
    }


    @PostMapping("/add")
    @ApiOperation(value = "新增公排分类")
    public ApiResult<?> addClass(@RequestBody LbLineClass bean){
        if (StringUtils.isEmpty(bean.getTitle())){
            return ApiResult.fail("分类名称不能为空");
        }
        if (null == bean.getStatus()){
            return ApiResult.fail("状态不能为空");
        }
        if (bean.getStatus() == 1){
            List<LbLineClass> lineClasses = lineClassService.queryList();
            if (null != lineClasses && !lineClasses.isEmpty()){
                return ApiResult.fail("只能有一个任务进行");
            }
        }
        bean.setCreateTime(new Date());
        bean.setStoreId(LoginUtil.getUserId());
        boolean save = lineClassService.save(bean);
        if (save){
            return ApiResult.ok("新增成功");
        }
        return ApiResult.fail("新增失败");
    }



    @PostMapping("/edit")
    @ApiOperation(value = "编辑公排分类")
    public ApiResult<?> editClass(@RequestBody LbLineClass bean){
        if (null  == bean.getId()){
            return ApiResult.fail("分类ID不能为空");
        }
        if (StringUtils.isEmpty(bean.getTitle())){
            return ApiResult.fail("分类名称不能为空");
        }
        if (null == bean.getStatus()){
            return ApiResult.fail("状态不能为空");
        }

        if (bean.getStatus() == 1){
            List<LbLineClass> lineClasses = lineClassService.queryList();
            if (null != lineClasses && !lineClasses.isEmpty()){
                return ApiResult.fail("只能有一个任务进行");
            }
        }
        LbLineClass byId = lineClassService.getById(bean.getId());
        if (byId.getStatus() == 2){
            return ApiResult.fail("关闭的分类不能编辑");
       }
        byId.setStatus(bean.getStatus());
        byId.setTitle(bean.getTitle());
        if (bean.getStatus() == 2 && byId.getStatus() == 1){
            String s = lineClassService.stopLine(byId.getId());
            if (!"200".equals(s)){
                return  ApiResult.ok("编辑失败");
            }
        }

        boolean save = lineClassService.updateById(byId);
        if (save){
            return ApiResult.ok("编辑成功");
        }
        return ApiResult.fail("编辑失败");
    }

    @GetMapping("/close")
    @ApiOperation(value = "关闭当前分类公排")
    public ApiResult<?> closeClass(@RequestParam("id") Integer id){
        String s = lineClassService.stopLine(id);
        if ("200".equals(s)){
            return  ApiResult.ok("关闭成功");
        }
        return  ApiResult.fail(s);
    }

}
